package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bll.ITreeNode;
import dal.*;
import util.*;

public class ChecklistItemModel implements IModel, ITreeNode {

	public static class FieldNames {
		public static String Id = "Id";
		public static String IdChecklist = "IdChecklist";
		public static String DisplayIndex = "DisplayIndex";
		public static String Name = "Name";
		public static String Remark = "Remark";
		public static String IsGroup = "IsGroup";
		public static String IdEmployee = "IdEmployee";
		public static String IdCompany = "IdCompany";
		public static String StartTime = "StartTime";
		public static String EndTime = "EndTime";
		public static String Status = "Status";
		public static String Index = "Index";
		public static String Type = "Type";
		public static String IsDraft = "IsDraft";
		public static String Notification = "Notification";
		public static String Created = "Created";
		public static String Creator = "Creator";
		public static String Updated = "Updated";
		public static String Updator = "Updator";
		public static String Deleted = "Deleted";
		public static String DeletionTime = "DeletionTime";
		public static String IdParent = "IdParent";
		public static String IdPhase = "IdPhase";
	}

	public class ErrorMessages {
		public String Id = "";
		public String IdChecklist = "";
		public String DisplayIndex = "";
		public String Name = "";
		public String Remark = "";
		public String IsGroup = "";
		public String IdEmployee = "";
		public String IdCompany = "";
		public String StartTime = "";
		public String EndTime = "";
		public String Status = "";
		public String Index = "";
		public String Type = "";
		public String IsDraft = "";
		public String Notification = "";
		public String Created = "";
		public String Creator = "";
		public String Updated = "";
		public String Updator = "";
		public String Deleted = "";
		public String DeletionTime = "";
		public String IdParent = "";
		public String IdPhase = "";

	}

	public ErrorMessages errMsgs = new ErrorMessages();

	public String[] errMsgList = null;

	// private variables
	private int _id = Integer.MIN_VALUE;
	private int _id_checklist = Integer.MIN_VALUE;
	private String _display_index = null;
	private String _name = null;
	private String _remark = null;
	private boolean _is_group = false;
	private boolean _isis_groupSet = false;
	private int _id_employee = Integer.MIN_VALUE;
	private int _id_company = Integer.MIN_VALUE;
	private Date _start_time = null;
	private Date _end_time = null;
	private int _status = Integer.MIN_VALUE;
	private int _index = Integer.MIN_VALUE;
	private int _type = Integer.MIN_VALUE;
	private boolean _is_draft = false;
	private boolean _isis_draftSet = false;
	private int _notification = Integer.MIN_VALUE;
	private Date _created = null;
	private int _creator = Integer.MIN_VALUE;
	private Date _updated = null;
	private int _updator = Integer.MIN_VALUE;
	private boolean _deleted = false;
	private boolean _isdeletedSet = false;
	private Date _deletion_time = null;
	private int _id_parent = Integer.MIN_VALUE;
	private int _id_phase = Integer.MIN_VALUE;

	public ChecklistItemModel() {
	}

	// get/set
	public int getId() {
		return _id;
	}

	public void setId(int value) {
		_id = value;
	}

	public String getIdToString() {
		if (_id == Integer.MIN_VALUE || _id == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id);
		}
	}

	public int getIdChecklist() {
		return _id_checklist;
	}

	public void setIdChecklist(int value) {
		_id_checklist = value;
	}

	public String getIdChecklistToString() {
		if (_id_checklist == Integer.MIN_VALUE || _id_checklist == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_checklist);
		}
	}

	public String getDisplayIndex() {
		return _display_index;
	}

	public void setDisplayIndex(String value) {
		_display_index = value;
	}

	public String getDisplayIndexToString() {
		if (_display_index != null) {
			return HtmlUtil.genHtml(_display_index);
		} else {
			return "";
		}
	}

	public String getName() {
		return _name;
	}

	public void setName(String value) {
		_name = value;
	}

	public String getNameToString() {
		if (_name != null) {
			return HtmlUtil.genHtml(_name);
		} else {
			return "";
		}
	}

	public String getRemark() {
		return _remark;
	}

	public void setRemark(String value) {
		_remark = value;
	}

	public String getRemarkToString() {
		if (_remark != null) {
			return HtmlUtil.genHtml(_remark);
		} else {
			return "";
		}
	}

	public boolean getIsGroup() {
		return _is_group;
	}

	public void setIsGroup(boolean value) {
		_is_group = value;
		_isis_groupSet = true;
	}

	public boolean getIsGroupSet() {
		return _isis_groupSet;
	}

	public String getIsGroupToString() {
		if (_isis_groupSet) {
			return HtmlUtil.genHtml(_is_group);
		} else {
			return "";
		}
	}

	public int getIdEmployee() {
		return _id_employee;
	}

	public void setIdEmployee(int value) {
		_id_employee = value;
	}

	public String getIdEmployeeToString() {
		if (_id_employee == Integer.MIN_VALUE || _id_employee == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_employee);
		}
	}

	public int getIdCompany() {
		return _id_company;
	}

	public void setIdCompany(int value) {
		_id_company = value;
	}

	public String getIdCompanyToString() {
		if (_id_company == Integer.MIN_VALUE || _id_company == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_company);
		}
	}

	public Date getStartTime() {
		return _start_time;
	}

	public void setStartTime(Date value) {
		_start_time = value;
	}

	public String getStartTimeToString() {
		if (_start_time != null) {
			return StringUtil.toDateTimeString(_start_time);
		} else {
			return "";
		}
	}

	public Date getEndTime() {
		return _end_time;
	}

	public void setEndTime(Date value) {
		_end_time = value;
	}

	public String getEndTimeToString() {
		if (_end_time != null) {
			return StringUtil.toDateTimeString(_end_time);
		} else {
			return "";
		}
	}

	public int getStatus() {
		return _status;
	}

	public void setStatus(int value) {
		_status = value;
	}

	public String getStatusToString() {
		if (_status == Integer.MIN_VALUE || _status == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_status);
		}
	}

	public int getIndex() {
		return _index;
	}

	public void setIndex(int value) {
		_index = value;
	}

	public String getIndexToString() {
		if (_index == Integer.MIN_VALUE || _index == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_index);
		}
	}

	public int getType() {
		return _type;
	}

	public void setType(int value) {
		_type = value;
	}

	public String getTypeToString() {
		if (_type == Integer.MIN_VALUE || _type == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_type);
		}
	}

	public boolean getIsDraft() {
		return _is_draft;
	}

	public void setIsDraft(boolean value) {
		_is_draft = value;
		_isis_draftSet = true;
	}

	public boolean getIsDraftSet() {
		return _isis_draftSet;
	}

	public String getIsDraftToString() {
		if (_isis_draftSet) {
			return HtmlUtil.genHtml(_is_draft);
		} else {
			return "";
		}
	}

	public int getNotification() {
		return _notification;
	}

	public void setNotification(int value) {
		_notification = value;
	}

	public String getNotificationToString() {
		if (_notification == Integer.MIN_VALUE || _notification == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_notification);
		}
	}

	public Date getCreated() {
		return _created;
	}

	public void setCreated(Date value) {
		_created = value;
	}

	public String getCreatedToString() {
		if (_created != null) {
			return StringUtil.toDateTimeString(_created);
		} else {
			return "";
		}
	}

	public int getCreator() {
		return _creator;
	}

	public void setCreator(int value) {
		_creator = value;
	}

	public String getCreatorToString() {
		if (_creator == Integer.MIN_VALUE || _creator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_creator);
		}
	}

	public Date getUpdated() {
		return _updated;
	}

	public void setUpdated(Date value) {
		_updated = value;
	}

	public String getUpdatedToString() {
		if (_updated != null) {
			return StringUtil.toDateTimeString(_updated);
		} else {
			return "";
		}
	}

	public int getUpdator() {
		return _updator;
	}

	public void setUpdator(int value) {
		_updator = value;
	}

	public String getUpdatorToString() {
		if (_updator == Integer.MIN_VALUE || _updator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_updator);
		}
	}

	public boolean getDeleted() {
		return _deleted;
	}

	public void setDeleted(boolean value) {
		_deleted = value;
		_isdeletedSet = true;
	}

	public boolean getDeletedSet() {
		return _isdeletedSet;
	}

	public String getDeletedToString() {
		if (_isdeletedSet) {
			return HtmlUtil.genHtml(_deleted);
		} else {
			return "";
		}
	}

	public Date getDeletionTime() {
		return _deletion_time;
	}

	public void setDeletionTime(Date value) {
		_deletion_time = value;
	}

	public String getDeletionTimeToString() {
		if (_deletion_time != null) {
			return StringUtil.toDateTimeString(_deletion_time);
		} else {
			return "";
		}
	}

	public int getIdParent() {
		return _id_parent;
	}

	public void setIdParent(int value) {
		_id_parent = value;
	}

	public String getIdParentToString() {
		if (_id_parent == Integer.MIN_VALUE || _id_parent == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_parent);
		}
	}

	public int getIdPhase() {
		return _id_phase;
	}

	public void setIdPhase(int value) {
		_id_phase = value;
	}

	public String getIdPhaseToString() {
		if (_id_phase == Integer.MIN_VALUE || _id_phase == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_phase);
		}
	}

	@Override
	public int[] getPrimaryKey() {
		int[] ids = new int[1];
		ids[0] = getId();
		return ids;
	}

	@Override
	public IModel readFromResultSet(ResultSet rs) throws SQLException {
		ChecklistItemModel model = null;

		model = new ChecklistItemModel();

		// read fields
		{
			int value = rs.getInt("id");
			if (!rs.wasNull()) {
				model.setId(value);
			}
		}
		{
			int value = rs.getInt("id_checklist");
			if (!rs.wasNull()) {
				model.setIdChecklist(value);
			}
		}
		{
			String value = rs.getString("display_index");
			if (!rs.wasNull()) {
				model.setDisplayIndex(value);
			}
		}
		{
			String value = rs.getString("name");
			if (!rs.wasNull()) {
				model.setName(value);
			}
		}
		{
			String value = rs.getString("remark");
			if (!rs.wasNull()) {
				model.setRemark(value);
			}
		}
		{
			boolean value = rs.getBoolean("is_group");
			if (!rs.wasNull()) {
				model.setIsGroup(value);
			}
		}
		{
			int value = rs.getInt("id_employee");
			if (!rs.wasNull()) {
				model.setIdEmployee(value);
			}
		}
		{
			int value = rs.getInt("id_company");
			if (!rs.wasNull()) {
				model.setIdCompany(value);
			}
		}
		{
			Date value = rs.getTimestamp("start_time");
			if (!rs.wasNull()) {
				model.setStartTime(value);
			}
		}
		{
			Date value = rs.getTimestamp("end_time");
			if (!rs.wasNull()) {
				model.setEndTime(value);
			}
		}
		{
			int value = rs.getInt("status");
			if (!rs.wasNull()) {
				model.setStatus(value);
			}
		}
		{
			int value = rs.getInt("index");
			if (!rs.wasNull()) {
				model.setIndex(value);
			}
		}
		{
			int value = rs.getInt("type");
			if (!rs.wasNull()) {
				model.setType(value);
			}
		}
		{
			boolean value = rs.getBoolean("is_draft");
			if (!rs.wasNull()) {
				model.setIsDraft(value);
			}
		}
		{
			int value = rs.getInt("notification");
			if (!rs.wasNull()) {
				model.setNotification(value);
			}
		}
		{
			Date value = rs.getTimestamp("created");
			if (!rs.wasNull()) {
				model.setCreated(value);
			}
		}
		{
			int value = rs.getInt("creator");
			if (!rs.wasNull()) {
				model.setCreator(value);
			}
		}
		{
			Date value = rs.getTimestamp("updated");
			if (!rs.wasNull()) {
				model.setUpdated(value);
			}
		}
		{
			int value = rs.getInt("updator");
			if (!rs.wasNull()) {
				model.setUpdator(value);
			}
		}
		{
			boolean value = rs.getBoolean("deleted");
			if (!rs.wasNull()) {
				model.setDeleted(value);
			}
		}
		{
			Date value = rs.getTimestamp("deletion_time");
			if (!rs.wasNull()) {
				model.setDeletionTime(value);
			}
		}
		{
			int value = rs.getInt("id_parent");
			if (!rs.wasNull()) {
				model.setIdParent(value);
			}
		}
		{
			int value = rs.getInt("id_phase");
			if (!rs.wasNull()) {
				model.setIdPhase(value);
			}
		}

		return model;
	}

	@Override
	public PreparedStatement genSelectStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "SELECT * FROM sp_checklist_item_select(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModel obj = (ChecklistItemModel) query;

		// set parameters
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genSelectListStatement(Connection dbConn, IModelExt query) throws SQLException {
		String sql = "SELECT * FROM sp_checklist_item_selectList(:limit, :offset, :id,:id_checklist,:display_index,:name,:remark,:is_group,:id_employee,:id_company,:start_timeFrom,:start_timeTo,:end_timeFrom,:end_timeTo,:status,:index,:type,:is_draft,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo,:id_parent,:id_phase)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModelExt obj = (ChecklistItemModelExt) query;

		if (obj.getLimit() != Integer.MIN_VALUE) {
			st.setObject("limit", obj.getLimit());
		} else {
			st.setObject("limit", Integer.MAX_VALUE);
		}
		if (obj.getOffset() != Integer.MIN_VALUE) {
			st.setObject("offset", obj.getOffset());
		} else {
			st.setObject("offset", 0);
		}

		// set parameters
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
		}
		if (obj.getDisplayIndex() != null) {
			st.setObject("display_index", obj.getDisplayIndex());
		} else {
			st.setObject("display_index", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getStartTimeFrom() != null) {
			st.setObject("start_timeFrom", obj.getStartTimeFrom());
		} else {
			st.setObject("start_timeFrom", null);
		}
		if (obj.getStartTimeTo() != null) {
			st.setObject("start_timeTo", obj.getStartTimeTo());
		} else {
			st.setObject("start_timeTo", null);
		}
		if (obj.getEndTimeFrom() != null) {
			st.setObject("end_timeFrom", obj.getEndTimeFrom());
		} else {
			st.setObject("end_timeFrom", null);
		}
		if (obj.getEndTimeTo() != null) {
			st.setObject("end_timeTo", obj.getEndTimeTo());
		} else {
			st.setObject("end_timeTo", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getIndex() != Integer.MIN_VALUE) {
			st.setObject("index", obj.getIndex());
		} else {
			st.setObject("index", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
			st.setObject("createdTo", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}
		if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
			st.setObject("updatedTo", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
			st.setObject("deletion_timeTo", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getIdPhase() != Integer.MIN_VALUE) {
			st.setObject("id_phase", obj.getIdPhase());
		} else {
			st.setObject("id_phase", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genInsertStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_checklist_item_insert(:id_checklist,:display_index,:name,:remark,:is_group,:id_employee,:id_company,:start_time,:end_time,:status,:index,:type,:is_draft,:notification,:creator,:id_parent,:id_phase)";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModel obj = (ChecklistItemModel) model;

		// set parameters here
		if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
		}
		if (obj.getDisplayIndex() != null) {
			st.setObject("display_index", obj.getDisplayIndex());
		} else {
			st.setObject("display_index", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getStartTime() != null) {
			st.setObject("start_time", obj.getStartTime());
		} else {
			st.setObject("start_time", null);
		}
		if (obj.getEndTime() != null) {
			st.setObject("end_time", obj.getEndTime());
		} else {
			st.setObject("end_time", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getIndex() != Integer.MIN_VALUE) {
			st.setObject("index", obj.getIndex());
		} else {
			st.setObject("index", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getIdPhase() != Integer.MIN_VALUE) {
			st.setObject("id_phase", obj.getIdPhase());
		} else {
			st.setObject("id_phase", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genUpdateStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_checklist_item_update(:id,:id_checklist,:display_index,:name,:remark,:is_group,:id_employee,:id_company,:start_time,:end_time,:status,:index,:type,:is_draft,:notification,:updator,:deleted,:deletion_time,:id_parent,:id_phase)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModel obj = (ChecklistItemModel) model;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
		}
		if (obj.getDisplayIndex() != null) {
			st.setObject("display_index", obj.getDisplayIndex());
		} else {
			st.setObject("display_index", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getStartTime() != null) {
			st.setObject("start_time", obj.getStartTime());
		} else {
			st.setObject("start_time", null);
		}
		if (obj.getEndTime() != null) {
			st.setObject("end_time", obj.getEndTime());
		} else {
			st.setObject("end_time", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getIndex() != Integer.MIN_VALUE) {
			st.setObject("index", obj.getIndex());
		} else {
			st.setObject("index", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTime() != null) {
			st.setObject("deletion_time", obj.getDeletionTime());
		} else {
			st.setObject("deletion_time", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getIdPhase() != Integer.MIN_VALUE) {
			st.setObject("id_phase", obj.getIdPhase());
		} else {
			st.setObject("id_phase", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genDeleteStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "select sp_checklist_item_delete(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModel obj = (ChecklistItemModel) query;

		// set primary keys
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genCountStatement(Connection dbConn, IModelExt input) throws SQLException {
		String sql = "SELECT sp_checklist_item_count(:id,:id_checklist,:display_index,:name,:remark,:is_group,:id_employee,:id_company,:start_timeFrom,:start_timeTo,:end_timeFrom,:end_timeTo,:status,:index,:type,:is_draft,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo,:id_parent,:id_phase)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChecklistItemModelExt obj = (ChecklistItemModelExt) input;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
		}
		if (obj.getDisplayIndex() != null) {
			st.setObject("display_index", obj.getDisplayIndex());
		} else {
			st.setObject("display_index", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getStartTimeFrom() != null) {
			st.setObject("start_timeFrom", obj.getStartTimeFrom());
		} else {
			st.setObject("start_timeFrom", null);
		}
		if (obj.getStartTimeTo() != null) {
			st.setObject("start_timeTo", obj.getStartTimeTo());
		} else {
			st.setObject("start_timeTo", null);
		}
		if (obj.getEndTimeFrom() != null) {
			st.setObject("end_timeFrom", obj.getEndTimeFrom());
		} else {
			st.setObject("end_timeFrom", null);
		}
		if (obj.getEndTimeTo() != null) {
			st.setObject("end_timeTo", obj.getEndTimeTo());
		} else {
			st.setObject("end_timeTo", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getIndex() != Integer.MIN_VALUE) {
			st.setObject("index", obj.getIndex());
		} else {
			st.setObject("index", null);
		}
		if (obj.getType() != Integer.MIN_VALUE) {
			st.setObject("type", obj.getType());
		} else {
			st.setObject("type", null);
		}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
			st.setObject("createdTo", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}
		if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
			st.setObject("updatedTo", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
			st.setObject("deletion_timeTo", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getIdPhase() != Integer.MIN_VALUE) {
			st.setObject("id_phase", obj.getIdPhase());
		} else {
			st.setObject("id_phase", null);
		}

		return st.getPreparedStatement();
	}

	/*
	 * @Override public String toHtml() { // TODO Auto-generated method stub
	 * return null; }
	 */

	/*
	 * @Override public String fillHtmlTemplate(String template) { // TODO
	 * Auto-generated method stub return null; }
	 */

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":id", Integer.toString(_id)) .replace(":id_checklist",
	 * Integer.toString(_id_checklist)) .replace(":display_index",
	 * HtmlUtil.genHtml(_display_index)) .replace(":name",
	 * HtmlUtil.genHtml(_name)) .replace(":remark", HtmlUtil.genHtml(_remark))
	 * .replace(":is_group", HtmlUtil.genHtml(_isis_groupSet, _is_group))
	 * .replace(":id_employee", Integer.toString(_id_employee))
	 * .replace(":id_company", Integer.toString(_id_company))
	 * .replace(":start_time", StringUtil.toDateTimeString(_start_time))
	 * .replace(":end_time", StringUtil.toDateTimeString(_end_time))
	 * .replace(":status", Integer.toString(_status)) .replace(":index",
	 * Integer.toString(_index)) .replace(":type", Integer.toString(_type))
	 * .replace(":is_draft", HtmlUtil.genHtml(_isis_draftSet, _is_draft))
	 * .replace(":notification", Integer.toString(_notification))
	 * .replace(":created", StringUtil.toDateTimeString(_created))
	 * .replace(":creator", Integer.toString(_creator)) .replace(":updated",
	 * StringUtil.toDateTimeString(_updated)) .replace(":updator",
	 * Integer.toString(_updator)) .replace(":deleted",
	 * HtmlUtil.genHtml(_isdeletedSet, _deleted)) .replace(":deletion_time",
	 * StringUtil.toDateTimeString(_deletion_time)) .replace(":id_parent",
	 * Integer.toString(_id_parent))
	 * 
	 * ; return html; }
	 */

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":Id", Integer.toString(_Id)) .replace(":IdChecklist",
	 * Integer.toString(_IdChecklist)) .replace(":DisplayIndex",
	 * HtmlUtil.genHtml(_DisplayIndex)) .replace(":Name",
	 * HtmlUtil.genHtml(_Name)) .replace(":Remark", HtmlUtil.genHtml(_Remark))
	 * .replace(":IsGroup", HtmlUtil.genHtml(_isIsGroupSet, _IsGroup))
	 * .replace(":IdEmployee", Integer.toString(_IdEmployee))
	 * .replace(":IdCompany", Integer.toString(_IdCompany))
	 * .replace(":StartTime", StringUtil.toDateTimeString(_StartTime))
	 * .replace(":EndTime", StringUtil.toDateTimeString(_EndTime))
	 * .replace(":Status", Integer.toString(_Status)) .replace(":Index",
	 * Integer.toString(_Index)) .replace(":Type", Integer.toString(_Type))
	 * .replace(":IsDraft", HtmlUtil.genHtml(_isIsDraftSet, _IsDraft))
	 * .replace(":Notification", Integer.toString(_Notification))
	 * .replace(":Created", StringUtil.toDateTimeString(_Created))
	 * .replace(":Creator", Integer.toString(_Creator)) .replace(":Updated",
	 * StringUtil.toDateTimeString(_Updated)) .replace(":Updator",
	 * Integer.toString(_Updator)) .replace(":Deleted",
	 * HtmlUtil.genHtml(_isDeletedSet, _Deleted)) .replace(":DeletionTime",
	 * StringUtil.toDateTimeString(_DeletionTime)) .replace(":IdParent",
	 * Integer.toString(_IdParent))
	 * 
	 * ; return html; }
	 */

	// ADDED CODE

	private List<ChecklistItemModel> _childNodes = new ArrayList<ChecklistItemModel>();
	private ChecklistItemModel _parent = null;
	private int _level = 0;

	@Override
	public int compareTo(ITreeNode o) {
		ChecklistItemModel obj = (ChecklistItemModel) o;
		int res = StringUtil.compareStrings(_display_index, obj.getDisplayIndex());
		return res;
	}

	@Override
	public ITreeNode getParent() {
		return _parent;
	}

	@Override
	public void setParent(ITreeNode parent) {
		_parent = (ChecklistItemModel) parent;
	}

	@Override
	public List<ITreeNode> getChildNodes() {
		List<ITreeNode> res = new ArrayList<ITreeNode>();
		for (ChecklistItemModel obj : _childNodes) {
			res.add(obj);
		}
		return res;
	}

	@Override
	public int getLevel() {
		return _level;
	}

	@Override
	public void setLevel(int level) {
		_level = level;
	}

	@Override
	public void addChildNode(ITreeNode childNode) {
		_childNodes.add((ChecklistItemModel) childNode);
	}

	public String getPIC() throws Exception {
		String _PIC = StringUtil.displayStringArray(new String[] { CacheUtil.getEmployeeName(_id_employee), CacheUtil.getCompanyName(_id_company) }, "/");
		return _PIC;
	}

	private boolean _addedToTree = false;

	@Override
	public boolean getAddedToTree() {
		return _addedToTree;
	}

	@Override
	public void setAddedToTree(boolean added) {
		_addedToTree = added;
	}

	private Date _totalStartTime = null;
	private Date _totalEndTime = null;

	public Date getTotalStartTime() {
		if (_is_group) {
			if (_totalStartTime == null && _childNodes.size() > 0) {
				for (ChecklistItemModel child : _childNodes) {
					if ((child.getStartTime() == null) || (child.getStartTime() == child.getEndTime()))
						continue;
					else if (child.getIsGroup()) {
						Date childStart = child.getTotalStartTime();
						if (_totalStartTime == null || _totalStartTime.after(childStart)) {
							_totalStartTime = childStart;
						}
					} else {
						if (_totalStartTime == null || _totalStartTime.after(child.getStartTime())) {
							_totalStartTime = child.getStartTime();
						}
					}
				}
			}
		} else {
			_totalStartTime = _start_time;
		}

		return _totalStartTime;
	}

	// public void setTotalStartTime(Date _totalStartTime) {
	// this._totalStartTime = _totalStartTime;
	// }

	public Date getTotalEndTime() {
		if (_is_group) {
			if (_totalEndTime == null && _childNodes.size() > 0) {
				for (ChecklistItemModel child : _childNodes) {
					if ((child.getEndTime() == null) || (child.getStartTime() == child.getEndTime()))
						continue;
					else if (child.getIsGroup()) {
						Date childEnd = child.getTotalEndTime();
						if (_totalEndTime == null || _totalEndTime.before(childEnd)) {
							_totalEndTime = childEnd;
						}
					} else {
						if (_totalEndTime == null || _totalEndTime.before(child.getEndTime())) {
							_totalEndTime = child.getEndTime();
						}
					}
				}
			}
		} else {
			_totalEndTime = _end_time;
		}

		return _totalEndTime;
	}

	// public void setTotalEndTime(Date _totalEndTime) {
	// this._totalEndTime = _totalEndTime;
	// }
}